<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加减乘除计算器</title>
</head>
<body>
<input type="text" id="i1" placeholder="请输入数字1"> <br>
<input type="text" id="i2" placeholder="请输入数字2">
<h3>运算结果：<span class="result"></span></h3>
<button onclick="cal('+')">加</button>
<button onclick="cal('-')">减</button>
<button onclick="cal('*')">乘</button>
<button onclick="cal('/')">除</button>
<script>
    function cal(o){
        let spanE=document.querySelector('.result')
        let input1=document.querySelector('#i1');
        let input2=document.querySelector('#i2');
        let i1=parseFloat(input1.value);
        let i2=parseFloat(input2.value);
        switch (o) {
            case '+':
                spanE.innerHTML=i1+i2;
                break;
            case '-':
                spanE.innerHTML=i1-i2;
                break;
            case '*':
                spanE.innerHTML=i1*i2;
                break;
            case '/':
                if (i2==0){
                    alert("除数不能为0")
                }else{
                spanE.innerHTML=i1/i2;}
                break;

        }




    }
    function sub1(){
        let spanE=document.querySelector('.result')
        let input1=document.querySelector('.i1');
        let input2=document.querySelector('.i2');
        let i1=parseFloat(input1.value);
        let i2=parseFloat(input2.value);
        let total=i1-i2;
        spanE.innerHTML=total;

    }
    function mul1(){
        let spanE=document.querySelector('.result')
        let input1=document.querySelector('.i1');
        let input2=document.querySelector('.i2');
        let i1=parseFloat(input1.value);
        let i2=parseFloat(input2.value);
        let total=i1*i2;
        spanE.innerHTML=total;

    }
    function div1(){
        let spanE=document.querySelector('.result')
        let input1=document.querySelector('.i1');
        let input2=document.querySelector('.i2');
        let i1=parseFloat(input1.value);
        let i2=parseFloat(input2.value);
        if (i2==0){
            alert('除数不能为0')
        }else {
            let total = i1 / i2;
            spanE.innerHTML = total;
        }

    }
</script>
</body>
</html>